package cn.tansanqinger.service.file.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.file.FileMessageSqlDaoImpl;
import cn.tansanqinger.dao.sql.file.FileNameSqlDaoImpl;
import cn.tansanqinger.pojo.Filemessage;
import cn.tansanqinger.pojo.Filename;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.file.IFileDisposeService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class FileDisposeServiceImpl implements IFileDisposeService{

	@Autowired
	private FileNameSqlDaoImpl nimpl;
	@Autowired
	private FileMessageSqlDaoImpl mimpl;
	
	@Override
	public List<Filename> selectFilenameAll(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return nimpl.selectAll(user.getId());
	}

	@Override
	public List<Filemessage> selectFileMessageAll(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		String fid = Tools.isEmpty(request.getParameter("fid"));
		if(fid.matches("\\d+")) {
			return mimpl.selectAll(Integer.parseInt(fid), user.getId());
		}
		return mimpl.selectAll(0, user.getId());
	}

	@Override
	public Filename selectFilenameOne(int id) {
		return nimpl.selectFidOne(id);
	}

	@Override
	public Filemessage selectFilemessageOne(int id) {
		return mimpl.selectFidOne(id);
	}
	
	@Override
	public PageData addFileName(HttpServletRequest request) {
		Filename filename = new Filename();
		PageData pd = new PageData();
		User user = (User) request.getSession().getAttribute("user");
		filename.setUid(user.getId());
		filename.setFdate(DateUtil.getTime());
		filename.setFtime(System.currentTimeMillis()/1000);
		filename.setFname(Tools.isEmpty(request.getParameter("name")));
		filename.setFcapacity(0.0);
		int fid = nimpl.add(filename);
		if(fid>0) {
			pd.put("code", "0");
			pd.put("msg", "上传成功");
			pd.put("fid", fid);
			pd.put("fname", filename.getFname());
		} else {
			pd.put("code", "-1");
			pd.put("msg", "上传失败");
		}
		return pd;
	}

	@Override
	public int addFileMessage(Filemessage filemessage) {
		return mimpl.add(filemessage);
	}

	@Override
	public PageData modificationFileName(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			Filename filename = selectFilenameOne(Integer.parseInt(id));
			filename.setFname(Tools.isEmpty(request.getParameter("name")));
			if(nimpl.modification(filename)==1) {
				pd.put("code", "0");
				pd.put("msg", "修改成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "修改失败");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

	@Override
	public PageData modificationFileMessage(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			Filemessage filemessage = selectFilemessageOne(Integer.parseInt(id));
			filemessage.setFname(Tools.isEmpty(request.getParameter("name")));
			if(mimpl.modification(filemessage)==1) {
				pd.put("code", "0");
				pd.put("msg", "修改成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "修改失败");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

	@Override
	public PageData modificationFileToNewFile(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			Filemessage filemessage = selectFilemessageOne(Integer.parseInt(id));
			filemessage.setFid(Integer.parseInt(Tools.isEmpty(request.getParameter("fid"))));
			if(mimpl.modification(filemessage)==1) {
				pd.put("code", "0");
				pd.put("msg", "修改成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "修改失败");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

	@Override
	public PageData deleteFileName(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			if(nimpl.delete(Integer.parseInt(id))==1) {
				pd.put("code", "0");
				pd.put("msg", "删除成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "删除失败");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

	@Override
	public PageData deleteFileMessage(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pd = new PageData();
		if(id.matches("\\d+")) {
			if(mimpl.delete(Integer.parseInt(id))==1) {
				pd.put("code", "0");
				pd.put("msg", "删除成功");
			} else {
				pd.put("code", "-1");
				pd.put("msg", "删除失败");
			}
		} else {
			pd.put("code", "-1");
			pd.put("msg", "数据错误");
		}
		return pd;
	}

}
